#!/usr/bin/env ipython

import os
import sys

sys.path.append('../../lib')
sys.path.append(os.getenv("HOME")+'/lib')

from flgrcore import *
from flgrmorpho import *
from flgrrtview import *
from flgrrtcapture import *

def fas(imout,imin,nhb,sizemax):
    imtmp1 = FLGR_Data2D(imout.size_y,imout.size_x,FLGR_UINT8)
    imtmp2 = FLGR_Data2D(imout.size_y,imout.size_x,FLGR_UINT8)
    flgr2d_copy(imtmp1,imin)

    for i in range(sizemax-1):
        nhb1 = FLGR_Data2D(i*2+1,i*2+1,nhb.type,nhb.shape,FLGR_8_CONNEX)
        flgr2d_open(imtmp2,imtmp1,nhb1)
        flgr2d_close(imtmp1,imtmp2,nhb1)

    flgr2d_open(imtmp2,imtmp1,nhb1)
    flgr2d_close(imout,imtmp2,nhb1)

################################################# 
### MAIN ########################################
#################################################

cam0 = FLGR_RtCapture2D('/dev/video0')

cam0.set_palette('YUV420P')
    
size_x, size_y = cam0.get_size()

imin = FLGR_Data2D(size_y,size_x,FLGR_UINT8)
imout = FLGR_Data2D(size_y,size_x,FLGR_UINT8)
imtmp = FLGR_Data2D(size_y,size_x,FLGR_UINT8)
nhb1 = FLGR_Data2D(9,9,imin.type,FLGR_OCTAGON,FLGR_8_CONNEX)

scr0 = flgr2d_rtview_create(imin)

for i in range(2000):
    flgr2d_rtcapture_gray_image(imin, cam0)
    flgr2d_close(imtmp,imin, nhb1)
    flgr2d_close(imout,imtmp,nhb1)
    flgr2d_close(imtmp,imout,nhb1)
    flgr2d_close(imout,imtmp,nhb1)
    flgr2d_close(imtmp,imout,nhb1)
    flgr2d_close(imout,imtmp,nhb1)
    flgr2d_rtview_refresh(scr0, imout)
